package com.tao.base

import com.utry.lib.annotation.data.ComponentData
import java.io.IOException
import java.util.*
import java.util.concurrent.*

/**
 * @description: lab
 * @author: wangxf
 * @date: 2023/5/19 10:33
 */
object ModuleController {
    private const val maxRequests = 64

    private var executorService: ExecutorService? = null




    private val readyAsyncCalls: Deque<Call> = ArrayDeque<Call>()
    private val runningAsyncCalls: Deque<Call> = ArrayDeque<Call>()
    private val runningSyncCalls: Deque<Call> = ArrayDeque<Call>()


    @Synchronized
    fun executorService(): ExecutorService? {
        if (this.executorService == null) {
            this.executorService = ThreadPoolExecutor(
                0,
                2147483647,
                60L,
                TimeUnit.SECONDS,
                SynchronousQueue()
            )
        }
        return this.executorService
    }

    @Synchronized
    fun enqueue(call: Call) {
        if (runningAsyncCalls.size < this.maxRequests) {
            runningAsyncCalls.add(call)
            executorService()!!.execute(call)
        } else {
            readyAsyncCalls.add(call)
        }
    }

    @Synchronized
    fun executed(call: Call) {
        runningSyncCalls.add(call)
    }

}

class Call(data: ComponentData, val callback: Callback) : Runnable {
    override fun run() {
        /** TODO:   1.判断是否是环境组件：环境组件直接初始化
         *          2.功能组件判断模块初始化先后顺序，不具备则跳过
         *          3.依托的环境组件判断初始化状态，执行初始化操作
         *          4.创建入口类实例
         *          5.判断初始化线程模型
         *          6.分发初调用 初始化任务方法
         */



    }
}

interface Callback {
    fun onFailure(var1: Call, var2: IOException?)

    fun onReset(var1: Call)

    @Throws(IOException::class)
    fun onFinish(var1: Call)
}
